<?php 
namespace Admin\Controller;
use Common\Base\ControllerBase;

class FrameworkController extends ControllerBase
{
    public $debug_file;
    
    public function _initialize()
    {
        $this->debug_file = $_SERVER['DOCUMENT_ROOT'].'/debug';
    }
    
    public function cache()
    {
        $cache_module = C('MODULE_ALLOW_LIST');
        $this->assign('module',$cache_module);
        $this->display();
    }

    //清除 /Runtime/common~runtime.php 文件
    public function clear_common_runtime()
    {
        $file = RUNTIME_PATH.'/common~runtime.php';
        if(is_file($file)){
            @unlink($file);
        }
        $this->ajaxReturn(array('st'=>'y', 'note'=>'ok'));
    }

    //清除 /Runtime/Runtime/Temp下的文件
    public function clear_runtime_temp()
    {
        $dir = RUNTIME_PATH.'/Temp';
        if(is_dir($dir)){
            delFile($dir,'php');
        }
        $this->ajaxReturn(array('st'=>'y', 'note'=>'ok'));
    }

    //清除 /Runtime/Runtime/Data下的文件
    public function clear_runtime_data()
    {
        $dir = RUNTIME_PATH.'/Data';
        if(is_dir($dir)){
            delFile($dir,'php');
        }
        $this->ajaxReturn(array('st'=>'y', 'note'=>'ok'));
    }
    
    //清除 /Public/js/common/init.js 文件
    public function clear_public_init()
    {
        $file = $_SERVER['DOCUMENT_ROOT'].'/js/common/init.js';
        @unlink($file);
        $this->ajaxReturn(array('st'=>'y', 'note'=>'ok'));
    }

    //清除 /Runtime/cache/ 各目录下的文件
    public function clear_runtime_cache()
    {
        $module = I('post.module');
        $dir = RUNTIME_PATH.'Cache'.'/'.$module;
        $dh = opendir($dir);
        if($dh!=false){
            while($file=readdir($dh)){
                if($file != "." && $file != ".."){
                    $fullPath = $dir.'/'.$file;
                    if(!is_dir($fullPath)){
                        unlink($fullPath);
                    }
                }      
            } 
            $this->ajaxReturn(array('st'=>'y','note'=>'ok'));
        }else{
            $this->ajaxReturn(array('st'=>'n','note'=>'目录不存在'));   
        }
        closedir($dh);
    }

    //框架日志文件夹
    public function logs()
    {
        $dir = RUNTIME_PATH.'Logs';
        $file = scandir($dir);
        $dir_files = array();
        foreach($file as $v){
            if($v=='.' || $v=='..'){ continue; }
            if(is_dir($dir.'/'.$v)){ $dir_files[] = $v; }
        }
        $this->assign('dir', $dir);
        $this->assign('logsDir', $dir_files);
        $this->display();
    }

    //日志列表
    public function logs_files()
    {
        $d_name = I('get.d_name');
        $dir = RUNTIME_PATH.'Logs/'.$d_name;
        $array_file = array();
        if($handle = opendir($dir)){
            while(false !== ($file = readdir($handle))){
                if ($file != "." && $file != ".."){
                    $array_file[] = $file;
                }
            }
            closedir($handle);   
        }else{
            $this->error('目录不存在');
        }
        $this->assign('dir', $dir);
        $this->assign('d_name', $d_name);
        $this->assign('files', $array_file);
        $this->display();
    }

    //日志内容
    public function logs_files_content()
    {
        $d_name = I('get.d_name');
        $f_name = I('get.f_name');
        $dir = RUNTIME_PATH.'Logs/'.$d_name.'/'.$f_name.'.log';
        if(is_file($dir)){
           $content = file_get_contents($dir); 
        }else{
            $this->error('文件不存在');
        }
        $this->assign('content',$content);
        $this->assign('d_name',$d_name);
        $this->assign('f_name',$f_name);
        $this->display();
    }
    
    //清除日志
    public function del_files()
    {
        $fileList = I('post.files');
        if( ! empty($fileList)){
            foreach($fileList as $v){
                $filename = RUNTIME_PATH.'Logs/'.$v;
                if(is_file($filename) && $filename != "." && $filename != "..") {
                    unlink($filename);  
                }
            }          
            $this->ajaxReturn(genReturn('y', '删除成功'));
        }else{
            $this->ajaxReturn(genReturn('n', '请选择要删除的'));
        }
    }
    
    //调试模式
    public function debug()
    {
        $w = I('get.w', '');
        if(in_array($w, array('y', 'n'))){
            if($w == 'y'){
                if( ! is_file($this->debug_file)){
                    $bd = fopen($this->debug_file, 'x');
                    fwrite($bd, 'open');
                    fclose($bd);
                }
            }else{
                @unlink($this->debug_file);
            }
            $this->ajaxReturn(genReturn('y', 'ok'));
        }
        $this->assign('is_debug_mod', is_file($this->debug_file));
        $this->display();
    }
}